/**
 * 获取图片元素
 */
export const getImgElements = (itemElements) => {
  const imgElements = []
  itemElements.forEach((itemElement) => {
    const imgElement = itemElement.querySelector('img')
    if (imgElement) {
      imgElements.push(imgElement)
    }
  })
  return imgElements
}

/**
 * 生成所有图片链接
 */
export const generateImgUrls = (imgElements) => {
  return imgElements.map((imgElement) => {
    return imgElement.src
  })
}

/**
 * 等待加载图片完成
 */
export const waitImgLoad = (imgs) => {
  const promisesAll = []
  imgs.forEach((img, index) => {
    const promise = new Promise((resolve, reject) => {
      const imgElement = new Image()
      imgElement.src = img
      imgElement.onload = () => {
        resolve({
          img,
          index
        })
      }
      imgElement.onerror = () => {
        reject({
          img,
          index
        })
      }
    })
    promisesAll.push(promise)
  })
  return Promise.all(promisesAll)
}

/**
 * 获取最小高度
 */
export const getMinHeight = (columnHeightObj) => {
  const columnHeightArr = Object.values(columnHeightObj)
  return Math.min(...columnHeightArr)
}

/**
 * 获取最大高度
 */
export const getMaxHeight = (columnHeightObj) => {
  const columnHeightArr = Object.values(columnHeightObj)
  return Math.max(...columnHeightArr)
}

/**
 * 获取最小高度的列
 */
export const getMinHeightColumn = (columnHeightObj) => {
  let minHeight = getMinHeight(columnHeightObj)
  return Object.keys(columnHeightObj).find((key) => {
    return columnHeightObj[key] === minHeight
  })
}
